<script setup lang="ts">
import type { Product } from '@/types/mall'
import { formatPrice } from '@/utils/mall'

interface Props {
  product: Product
  layout?: 'grid' | 'list' // 网格或列表布局
  showActions?: boolean // 是否显示操作按钮
  showDiscount?: boolean // 是否显示折扣标签
  showRating?: boolean // 是否显示评分
  showSales?: boolean // 是否显示销量
  cardClass?: string // 自定义样式类
}

const props = withDefaults(defineProps<Props>(), {
  layout: 'grid',
  showActions: true,
  showDiscount: true,
  showRating: true,
  showSales: true,
  cardClass: ''
})

const emit = defineEmits<{
  click: [product: Product]
  addToCart: [product: Product]
  addToFavorite: [product: Product]
}>()

const cartStore = useCartStore()

// 计算折扣
const discount = computed(() => {
  if (!props.product.originalPrice || props.product.originalPrice <= props.product.defaultPrice) {
    return null
  }
  return Math.round((1 - props.product.defaultPrice / props.product.originalPrice) * 10)
})

// 点击商品
function handleClick() {
  emit('click', props.product)
}

// 添加到购物车
async function handleAddToCart(event: Event) {
  event.stopPropagation()
  try {
    const success = await (cartStore as any).addToCart(props.product)
    if (success) {
      uni.vibrateShort()
      emit('addToCart', props.product)
    }
  } catch (error) {
    console.error('添加到购物车失败:', error)
    uni.showToast({ title: '添加失败', icon: 'none' })
  }
}

// 添加到收藏
function handleAddToFavorite(event: Event) {
  event.stopPropagation()
  // TODO: 实现收藏功能
  uni.showToast({ title: '已添加到收藏', icon: 'success' })
  emit('addToFavorite', props.product)
}
</script>

<template>
  <view 
    :class="[
      'product-card',
      `product-card--${layout}`,
      cardClass
    ]"
    @click="handleClick"
  >
    <!-- 商品图片 -->
    <view class="product-image-container">
      <image 
        :src="product.images[0]" 
        class="product-image" 
        mode="aspectFit"
        :lazy-load="true"
      />
      
      <!-- 折扣标签 -->
      <view 
        v-if="showDiscount && discount" 
        class="discount-tag"
      >
        {{ discount }}折
      </view>
      
      <!-- 商品标签 -->
      <view 
        v-if="product.tags && product.tags.length > 0" 
        class="product-tags"
      >
        <text
          v-for="tag in product.tags.slice(0, 2)"
          :key="tag"
          class="tag"
        >
          {{ tag }}
        </text>
      </view>
    </view>

    <!-- 商品信息 -->
    <view class="product-info">
      <text class="product-name">{{ product.name }}</text>
      
      <text 
        v-if="layout === 'list'" 
        class="product-desc"
      >
        {{ product.description }}
      </text>
      
      <!-- 价格信息 -->
      <view class="product-price">
        <text class="current-price">{{ formatPrice(product.defaultPrice) }}</text>
        <text 
          v-if="product.originalPrice && product.originalPrice > product.defaultPrice" 
          class="original-price"
        >
          {{ formatPrice(product.originalPrice) }}
        </text>
      </view>
      
      <!-- 评分和销量 -->
      <view 
        v-if="showRating || showSales" 
        class="product-stats"
      >
        <view 
          v-if="showRating" 
          class="rating"
        >
          <wd-icon name="star-on" size="12px" color="#ffc107" />
          <text>{{ product.rating }}</text>
        </view>
        <text 
          v-if="showSales" 
          class="sales"
        >
          已售{{ product.sales }}+
        </text>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view 
      v-if="showActions" 
      class="product-actions"
    >
      <wd-button
        v-if="layout === 'list'"
        type="info"
        size="small"
        @click="handleAddToFavorite"
      >
        收藏
      </wd-button>
      
      <wd-button
        type="primary"
        size="small"
        @click="handleAddToCart"
      >
        <wd-icon 
          v-if="layout === 'grid'" 
          name="cart" 
          size="16px" 
          color="#fff" 
        />
        <text v-else>加购物车</text>
      </wd-button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.product-card {
  position: relative;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;

  &:active {
    transform: scale(0.98);
  }

  &--grid {
    .product-image-container {
      .product-image {
        width: 100%;
        height: 120px;
        background: #f8f9fa;
      }
    }

    .product-info {
      padding: 10px;

      .product-name {
        font-size: 14px;
        font-weight: 500;
        color: #333;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        margin-bottom: 8px;
        line-height: 1.4;
      }

      .product-price {
        margin-bottom: 6px;
      }
    }

    .product-actions {
      position: absolute;
      bottom: 10px;
      right: 10px;

      :deep(.wd-button) {
        min-width: 30px;
        height: 30px;
        border-radius: 50%;
        padding: 0;
      }
    }
  }

  &--list {
    display: flex;
    padding: 15px;

    .product-image-container {
      margin-right: 12px;

      .product-image {
        width: 80px;
        height: 80px;
        border-radius: 8px;
        background: #f8f9fa;
      }
    }

    .product-info {
      flex: 1;
      margin-right: 10px;

      .product-name {
        font-size: 14px;
        font-weight: 500;
        color: #333;
        margin-bottom: 4px;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }

      .product-desc {
        font-size: 12px;
        color: #999;
        margin-bottom: 8px;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }

      .product-price {
        margin-bottom: 6px;
      }
    }

    .product-actions {
      display: flex;
      flex-direction: column;
      gap: 8px;
      align-items: flex-end;

      :deep(.wd-button) {
        min-width: 70px;
      }
    }
  }
}

.product-image-container {
  position: relative;

  .discount-tag {
    position: absolute;
    top: 8px;
    left: 8px;
    background: #ff4142;
    color: white;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 8px;
    z-index: 1;
  }

  .product-tags {
    position: absolute;
    bottom: 8px;
    left: 8px;
    display: flex;
    gap: 4px;

    .tag {
      font-size: 10px;
      color: #ff4142;
      background: rgba(255, 65, 66, 0.1);
      border: 1px solid #ff4142;
      border-radius: 2px;
      padding: 1px 4px;
    }
  }
}

.product-price {
  display: flex;
  align-items: center;
  gap: 6px;

  .current-price {
    font-size: 16px;
    font-weight: bold;
    color: #ff4142;
  }

  .original-price {
    font-size: 12px;
    color: #999;
    text-decoration: line-through;
  }
}

.product-stats {
  display: flex;
  align-items: center;
  gap: 12px;

  .rating {
    display: flex;
    align-items: center;
    gap: 2px;
    font-size: 11px;
    color: #666;
  }

  .sales {
    font-size: 11px;
    color: #999;
  }
}
</style>